package org.jeecg.modules.xxgx.common.util;

import org.apache.commons.lang.StringUtils;
import org.jeecg.modules.innerApi.common.requestDto.CommonParam;
import org.jeecg.modules.innerApi.common.requestDto.InvoiceParam;
import org.jeecg.modules.innerApi.common.requestDto.InvoicemxParam;
import org.jeecg.modules.xxgx.common.enums.TdyslxDmEnum;
import org.jeecg.modules.xxgx.common.vo.SplitDzdhVo;
import org.jeecg.modules.xxgx.common.vo.SplitYhkhVo;
import org.jeecg.modules.xxgx.djgl.entity.CsXxDkdjmx;
import org.jeecg.modules.xxgx.djgl.enums.ZsfsEnum;
import org.jeecg.modules.xxgx.djgl.vo.CsXxDkdjPage;
import org.jeecg.modules.xxgx.kpzdgl.service.ICsXxKpzdService;
import org.jeecg.modules.xxgx.spgl.entity.CsXxProduct;
import org.jeecg.modules.xxgx.spgl.service.ICsXxProductService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 开票数据转换工具类
 */

@Component
public class InvoiceConvertUtils {

    private static InvoiceConvertUtils convertUtils;

    @Autowired
    private ICsXxProductService spxxService;
    @Autowired
    private ICsXxKpzdService kpzdService;

    /**
     * 加载容器中资源
     * 项目启动生成调用
     */
    @PostConstruct
    private void init () {
        convertUtils = this;
    }


    /***
     * 将待开票单据转换为待开票信息
     * @param page
     * @return
     */
    public static InvoiceParam ConvertDjToFp(CsXxDkdjPage page){
        InvoiceParam invoiceParam=new InvoiceParam();
        //获取开票终端信息
        String kpzddm = page.getKpzddm();
        CommonParam commonParam=convertUtils.kpzdService.getCommonParamByKpzd(kpzddm);
        //完善开票终端、企业信息、服务器信息
        invoiceParam.setKpzd(commonParam.getKpzd());
        invoiceParam.setQyxx(commonParam.getQyxx());
        invoiceParam.setFwqxx(commonParam.getFwqxx());
        //设置请求流水号
        invoiceParam.setFpsqlsh(page.getId());
        BeanUtils.copyProperties(page,invoiceParam);
        invoiceParam.setFpmxs(convertDkdjmx(page.getCsXxDkdjmxList()));
        invoiceParam.setXfmc(commonParam.getKpzd().getNsrmc());
        invoiceParam.setXfsh(commonParam.getKpzd().getNsrsbh());
        invoiceParam.setXfdzdh(commonParam.getKpzd().getXfdz()+" "+commonParam.getKpzd().getXfdh());
        invoiceParam.setXfyhzh(commonParam.getKpzd().getXfyhzh());
        //设置清单标识
        List spmxs=page.getCsXxDkdjmxList();
        //清单发票
        if(spmxs!=null&&spmxs.size()>8){
            invoiceParam.setQdbz("1");
        }else{//非清单发票
            invoiceParam.setQdbz("0");
        }

        //设置扩展信息(特定业务)
        if(TdyslxDmEnum.JZFW.getValue().equals(page.getTdyslxdm())){
            invoiceParam.setTdywJzfw(page.getTdywJzfw());
        } else if(TdyslxDmEnum.HWYSFW.getValue().equals(page.getTdyslxdm())){
            invoiceParam.setTdywHwysfwList(page.getTdywHwysfwList());
        } else if(TdyslxDmEnum.BDCXS.getValue().equals(page.getTdyslxdm())){
            invoiceParam.setTdywBdcxs(page.getTdywBdcxs());
        } else if(TdyslxDmEnum.BDCJYZLFW.getValue().equals(page.getTdyslxdm())){
            invoiceParam.setTdywBdcjyzlfw(page.getTdywBdcjyzlfw());
        } else if(TdyslxDmEnum.LKYSFW.getValue().equals(page.getTdyslxdm())){
            invoiceParam.setTdywLkysfwList(page.getTdywLkysfwList());
        }

        //差额征收
        if(!ZsfsEnum.PTZS.getValue().equals(invoiceParam.getZsfs())){
            invoiceParam.setCezsList(page.getCezsList());
        }

        return invoiceParam;
    }

    /**
     * 将待开单据明细信息转换为待开发票明细信息
     * @param dkdjmxs
     * @return
     */
    private static List<InvoicemxParam> convertDkdjmx(List<CsXxDkdjmx> dkdjmxs){
        List<InvoicemxParam> invoicemxParams=new ArrayList<>();
        dkdjmxs.forEach(csXxDkdjmx -> {
            InvoicemxParam fpxxmx=new InvoicemxParam();
            BeanUtils.copyProperties(csXxDkdjmx,fpxxmx);
            //1.合并同一商品信息
            //2.商品折扣处理

            //3.转换商品名称
            String spmc=csXxDkdjmx.getSpmc();
            if(!spmc.startsWith("*")){//商品名称没有税收分类名称开头
                String spdm=csXxDkdjmx.getSpdm();
                String ssmc=csXxDkdjmx.getSsmc();
                if(StringUtils.isEmpty(ssmc)){
                    List<CsXxProduct> spxxs=convertUtils.spxxService.lambdaQuery().eq(CsXxProduct::getSpdm,spdm).list();
                    if(spxxs.size()>0) {
                        CsXxProduct spxx = spxxs.get(0);
                        ssmc= spxx.getSpssjc();
                    }
                }
                spmc="*"+ssmc+"*"+spmc;
                fpxxmx.setSpmc(spmc);
            }
            invoicemxParams.add(fpxxmx);
        });
        return invoicemxParams;
    }

    /**
     * 将地址电话拆分为地址、电话
     * @param dzdh
     */
    public static SplitDzdhVo splitDzdh(String dzdh){
        SplitDzdhVo splitDzdhVo = new SplitDzdhVo();
        String dz = null;
        String dh = null;
        if(StringUtils.isNotEmpty(dzdh)) {
            if (dzdh.contains(" ")) {
                String[] s = dzdh.split(" ");
                dz = s[0];
                dh = s[1];
            }else if(dzdh.contains(",")){
                String[] s = dzdh.split(",");
                dz = s[0];
                dh = s[1];
            } else {
                Pattern p = Pattern.compile("((\\d|-)+)$");
                Matcher m = p.matcher(dzdh);
                if (m.find()) {
                    dh = m.group(1).trim();
                    dz = dzdh.replace(dh, "").trim();
                }
            }
        }
        splitDzdhVo.setDz(dz);
        splitDzdhVo.setDh(dh);
        return splitDzdhVo;
    }

    /**
     * 将银行卡号拆分为银行名称、银行卡号
     * @param yhkh
     */
    public static SplitYhkhVo splitYhkh(String yhkh){
        SplitYhkhVo yhkhVo=new SplitYhkhVo();
        String khh = null;
        String yhzh = null;
        if(StringUtils.isNotEmpty(yhkh)) {
            if (yhkh.contains(" ")) {
                String[] s = yhkh.split(" ");
                khh = s[0];
                yhzh = s[1];
            } else if (yhkh.contains(",")) {
                String[] s = yhkh.split(",");
                khh = s[0];
                yhzh = s[1];
            } else {
                Pattern p = Pattern.compile("(\\d+)$");
                Matcher m = p.matcher(yhkh);
                if (m.find()) {
                    yhzh = m.group(1).trim();
                    khh = yhkh.replace(yhzh, "").trim();
                }
            }
        }
        yhkhVo.setYhkh(yhzh);
        yhkhVo.setYhmc(khh);
        return yhkhVo;
    }

}
